package HomeWork;
import java.util.Arrays;

/**
 * 本类用来演示: 员工类
 *
 * @author: success
 * @date: 2022/11/15 3:56 下午
 */
public class Employee {
    private int id;

    private String name;

    private double salary;

    //自关联的典型的案例
    //一个上司是对应多个下属的
    private Employee[] employees;

    public Employee(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Employee[] getEmployees() {
        return employees;
    }

    public void setEmployees(Employee[] employees) {
        this.employees = employees;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Employee{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", salary=").append(salary);
        sb.append('}');
        return sb.toString();
    }

    //三个业务方法
    public void addEmp(Employee employee){
        // boss.setEmployees(new Employee[0]);
        if(null==employees){
            System.out.println("sorry,您没有权限添加!");
            return;
        }
        //说明是有权限添加.
        //对数组进行扩容操作
        employees = Arrays.copyOf(employees,employees.length+1);
        employees[employees.length-1]=employee;
    }

    //输出所有的下属的信息
    public void findAll(){
        if(null!=employees && employees.length>0){
            for (Employee employee : employees) {
                System.out.println(employee);
            }
        }else{
            System.out.println("sorry,请先添加下属!");
        }
    }

    //根据id进行删除
    public void delById(int id){
        for(int i=0;i<=employees.length-1;i++){
            if(employees[i].getId()==id){
                if(i!=employees.length-1){
                    for(int j=i;j<employees.length-1;j++){
                        employees[j]=employees[j+1];
                    }
                }
                employees =Arrays.copyOf(employees,employees.length-1);
                return;
            }
        }

    }

    //开除薪资大于1500.0d
    public void delByPrice(double price){
        for(int i=0;i<=employees.length-1;i++){
            if(employees[i].getSalary()>=1500.0d){
                if(i!=employees.length-1) {
                    for (int j = i; j < employees.length - 1; j++) {
                        employees[j] = employees[j + 1];
                    }
                }
                employees = Arrays.copyOf(employees, employees.length - 1);

            }
        }

    }

}